﻿using System;
using AMS.Common;
using System.IO;

namespace AMS.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    public class InterfaceConfig
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        public InterfaceConfig(string fileName)
        {
            this.fileName = fileName;
        }

        private string fileName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public ListConfigInfo GetListConfig(string sectionName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("找不到配置文件" + fileName);
            }

            IniFile ini = new IniFile(fileName);
            if (!ini.IsSectionExists(sectionName))
            {
                throw new ConfigSectionNotExistException("配置节点" + sectionName + "不存在");
            }

            ListConfigInfo config = new ListConfigInfo();
            config.SectionName = sectionName;
            config.Title = ini.ReadValue(sectionName, "TITLE");
            config.ColumnFormat = ini.ReadValue(sectionName, "COLUMNS");
            config.CaptionFormat = ini.ReadValue(sectionName, "CAPTIONS");
            config.DisplayFormat = ini.ReadValue(sectionName, "DISPLAY");
            config.DisplayCaptionFormat = ini.ReadValue(sectionName, "DISPLAYCAPTIONS");
            config.ExportFormat = ini.ReadValue(sectionName, "EXPORT");
            config.ExportCaptionFormat = ini.ReadValue(sectionName, "EXPORTCAPTIONS");

            if (string.IsNullOrEmpty(config.ColumnFormat))
            {
                throw new ConfigException("关键信息COLUMNS不存在");
            }

            return config;
        }
    }
}
